package com.yanqu.road.dao.impl.servercenter;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.dao.impl.logic.ProcessInfoParser;
import com.yanqu.road.entity.servercenter.ProcessInfo;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class CrossProcessInfoDaoImpl extends TempDao {
    public List<ProcessInfo> getServerProcessInfo(int serverId) {
        ResultSet rs = null;
        CallableStatement call = null;
        List<ProcessInfo> list = new ArrayList<>();

        String sql = "select * from t_s_cross_server_process where crossId=?";
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(sql);
            call.setInt(1, serverId);
            rs = call.executeQuery();
            while (rs.next()) {
                ProcessInfo webServerInfo = ProcessInfoParser.parsingProcessInfo(rs.getString("webServerInfo"), rs.getInt("crossId"));
                ProcessInfo crossServerInfo = ProcessInfoParser.parsingProcessInfo(rs.getString("crossServerInfo"), rs.getInt("crossId"));
                list.add(webServerInfo);
                list.add(crossServerInfo);
                return list;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }


    public List<ProcessInfo> getAllCrossProcessInfo() {
        ResultSet rs = null;
        CallableStatement call = null;
        List<ProcessInfo> list = new ArrayList<>();

        String sql = "select * from t_s_cross_server_process";
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(sql);
            rs = call.executeQuery();
            while (rs.next()) {
                ProcessInfo crossServerInfo = ProcessInfoParser.parsingProcessInfo(rs.getString("crossServerInfo"), rs.getInt("crossId"));
                list.add(crossServerInfo);
                return list;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }

}
